<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * 用户中心
 * User_profile
 * @author: Skiychan <dev@skiy.net>
 *
 */

require(APPPATH.'libraries/Pc_Controller.php');
class User_profile extends Pc_Controller {
    private $_user_info;

    public function __construct() {
        parent::__construct();
        $services = array(
            'login_service',
            'user_info_service',
            'user_verify_service'
        );
        $this->load->service($services);
        $this->load->library("form_validation");
        $this->config->load("form_validation");
        $this->_user_info = $this->login_service->get_user_info();
    }

    /**
     * 取得用户信息
     */
    public function user_info_get() {
        #重新获取用户信息
        $this->login_service->reload_user_info();
        $this->_user_info = $this->login_service->get_user_info();
        #用户空间
        $user_space = array(
            'used_space' => $this->_user_info['used_space'],
            'capacity' => $this->_user_info['capacity']
        );
        $space = $this->user_info_service->get_format_space_info($user_space);

        //手机号及验证
        $mobile = $this->_user_info['n_mobile'];
        $v_mobile = 0;
        if (! empty($this->_user_info['mobile'])) {
            $mobile = $this->_user_info['mobile'];
            $v_mobile = 1;
        }

        //邮箱及验证
        $email = $this->_user_info['n_email'];
        $v_email = 0;
        if (! empty($this->_user_info['email'])) {
            $email = $this->_user_info['email'];
            $v_email = 1;
        }

        $result = array(
            'uid' => $this->_user_info['id'],
            'name' => $this->_user_info['nickname'],
            'company' => $this->_user_info['company_name'],
            'avatar' => $this->user_info_service->fetch_avatar($this->_user_info['image']),
            'mobile' => $mobile,
            'v_mobile' => $v_mobile,
            'email' => $email,
            'v_email' => $v_email,
            'space' => $space
        );

        return $this->common_response(0, '', array('data' =>$result));
    }

    /**
     * 获取用户空间容量
     */
    public function space_get() {
        $this->login_service->reload_user_info();
        $this->_user_info = $this->login_service->get_user_info();
        #用户空间
        $user_space = array(
            'used_space' => $this->_user_info['used_space'],
            'capacity' => $this->_user_info['capacity']
        );

        return $this->common_response(0, '', array('data' => $user_space));
    }

    /**
     * 退出
     */
    public function logout_get() {
        $this->login_service->logout();
        return $this->common_response($this->login_service);
    }
}
